package com.example.zmade;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zmade.service.BackService;

import java.util.Calendar;

import java.util.Timer;
import java.util.TimerTask;

import android_serialport_api.ComPort;

import static com.example.zmade.Func.getX1X2II;
import static com.example.zmade.Func.toUnShort;

public class MainActivity extends AppCompatActivity {
    private Button btn_setTemp,btn_addTemp,btn_reduceTemp,btn_open,btn_close;
    private TextView ShowTemp_tv,ShowTemp_tv_1,ShowTemp_tv_2;
    private EditText ControlTemp_et;
    private ImageView imageView;
    private float targetTemperature;
    private IMyAidlInterface myAidlInterface=new IMyAidlInterface.Default();
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myAidlInterface=IMyAidlInterface.Stub.asInterface(service);
        };

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    private void makeToastByRunOnUiThread(String str){
        MainActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private  void makeTextViewByRunOnUiThread(final TextView textView,String str)//更新UI
    {
        MainActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.setText(str);
            }
        });
    }

    private  void makeTextViewByRunOnUiThread(final EditText textView,String str)//更新UI
    {
        MainActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.setText(str);
            }
        });
    }

    private  void makeCloseByRunOnUiThread(final ImageView imageView)//更新UI
    {
        MainActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                imageView.setBackgroundDrawable(getResources().getDrawable(R.mipmap.close_1));
            }
        });
    }
    private  void makeOpenByRunOnUiThread(final ImageView imageView)//更新UI
    {
        MainActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                imageView.setBackgroundDrawable(getResources().getDrawable(R.mipmap.open_1));
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService(new Intent(MainActivity.this, BackService.class));
        bindService(new Intent(MainActivity.this, BackService.class), connection, Context.BIND_AUTO_CREATE);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
        window.setAttributes(params);

        Init();
        InitOnClick();

        Timer timer= new Timer();
        timer.schedule(timerTask_5,0,10000);
        timer.schedule(timerTask_4,100,3000);//获取设定温度
        timer.schedule(timerTask_3,200,1000);//判断开关门
        timer.schedule(timerTask_2,300,3000);//湿度
        timer.schedule(timerTask_1,400,3000);//温度

        ControlTemp_et.setText(targetTemperature+"");
    }
    public void Init(){
        btn_setTemp=findViewById(R.id.btnControlTemp);
        btn_addTemp=findViewById(R.id.btnControlTemp_2);
        btn_reduceTemp=findViewById(R.id.btnControlTemp_1);
        btn_open=findViewById(R.id.btn_cabinetOpen);
        btn_close=findViewById(R.id.btn_cabinetClose);
        ShowTemp_tv=findViewById(R.id.tvShowTemp);
        ShowTemp_tv_1=findViewById(R.id.tvShowTemp_1);
        ShowTemp_tv_2=findViewById(R.id.tvShowTemp_2);
        ControlTemp_et=findViewById(R.id.etControlTemp);
        imageView=findViewById(R.id.im_doorStatus);
    }
    public void InitOnClick(){
        btn_setTemp.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if ((ControlTemp_et.getText().toString().length() > 0) && (Float.parseFloat(ControlTemp_et.getText().toString()) > 1.9) && (Float.parseFloat(ControlTemp_et.getText().toString()) < 8.1)) {
                    try {
                        myAidlInterface.setTargetTemperature(Float.parseFloat(ControlTemp_et.getText().toString()));
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    makeToastByRunOnUiThread("发送完成");
                }
            }
        });
        /**
         * 温度+
         */
        btn_addTemp.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                float temp=Float.parseFloat(ControlTemp_et.getText().toString()) + 1;
                if(temp>8.0){
                    temp=(float)8.0;
                }
                else if(temp<2.0){
                    temp=(float)2.0;
                }
                ControlTemp_et.setText(temp+"");
            }
        });
        /**
         * 温度-
         */
        btn_reduceTemp.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                float temp=Float.parseFloat(ControlTemp_et.getText().toString()) - 1;
                if(temp>8.0){
                    temp=(float)8.0;
                }
                else if(temp<2.0){
                    temp=(float)2.0;
                }
                ControlTemp_et.setText(temp+"");
            }
        });
        btn_open.setOnClickListener(new View.OnClickListener(){//开门
            @Override
            public void onClick(View v) {
                try {
                    myAidlInterface.unlockCabinet();
                    makeOpenByRunOnUiThread(imageView);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
        btn_close.setOnClickListener(new View.OnClickListener() {//关门
            @Override
            public void onClick(View v) {
                try {
                    myAidlInterface.lockCabinet();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                makeCloseByRunOnUiThread(imageView);
            }
        });
    }
    TimerTask timerTask_1 = new TimerTask() {
        @Override
        public void run() {//获取温度
            if(handler!=null){
                Message msg=Message.obtain();
                try {
                    msg.obj=myAidlInterface.getTemperature();
                    msg.what=1;
                    handler.sendMessage(msg);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    TimerTask timerTask_2 = new TimerTask() {
        @Override
        public void run() {//获取湿度
            if(handler!=null){
                Message msg=Message.obtain();
                try {
                    msg.obj=myAidlInterface.getHumidity();
                    msg.what=2;
                    handler.sendMessage(msg);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    TimerTask timerTask_3 = new TimerTask() {
        @Override
        public void run() {//判断开关门
            if(handler!=null){
                Message msg=Message.obtain();
                try {
                    msg.obj=myAidlInterface.isCabinetUnlock();
                    msg.what=3;
                    handler.sendMessage(msg);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    TimerTask timerTask_4 = new TimerTask() {
        @Override
        public void run() {//自动更新设定温度
            if(handler!=null){
                Message msg=Message.obtain();
                try {
                    msg.obj=myAidlInterface.getTargetTemperature();
                    msg.what=4;
                    handler.sendMessage(msg);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    TimerTask timerTask_5 = new TimerTask() {
        @Override
        public void run() {//自动更新设定温度
            if(handler!=null){
                Message msg=Message.obtain();
                try {
                    msg.obj=myAidlInterface.getSystemTime();
                    msg.what=5;
                    handler.sendMessage(msg);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    public Handler handler=new Handler(){
        @Override
        public void handleMessage(android.os.Message msg) {
            if(msg.obj!=null){
                switch (msg.what){
                    case 1:float t=(float)msg.obj;
                        ShowTemp_tv.setText(t+"");
                        break;
                    case 2:
                        float h=(float)msg.obj;
                        ShowTemp_tv_2.setText(h+"");break;
                    case 3:boolean b=(boolean)msg.obj;
                    if(b){
                        makeOpenByRunOnUiThread(imageView);
                    }
                    else{
                        makeCloseByRunOnUiThread(imageView);
                    }
                        break;
                    case 4:
                        targetTemperature=(float)msg.obj;
                        ShowTemp_tv_1.setText(targetTemperature+"");
                        break;
                    case 5:
                        break;
                }
            }
        }
    };

}